using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Xml;
using NAVA.MTSLibrary;

namespace OpenMTS
{
    public partial class EvaluatorWindow : ToolWindow
    {
        #region MEMBERS
        private Hashtable htMessages = new Hashtable();
        private MessageTester mt = null;
        #endregion

        #region CONSTRUCTORS
        public EvaluatorWindow()
        {
            InitializeComponent();
            mt = MessageTester.MessageTesterFactory(@"C:\progs\test\proje\schemas");
            
            toolStripEvaluate.Enabled = false;
        }
        #endregion

        #region EVENTS
        public void ProjectChanged(ProjectFile project, ProjectFile.CHANGE_TYPE type, ProjectItem item)
        {
            if(item.FileType == ProjectFile.FILE_TYPE.message)
               ProjectLoad( project);
        }

        public void ProjectLoad(ProjectFile project)
        {
            //initialize droplist with input message file names
            this.cmbMessageFiles.Items.Clear();
            htMessages.Clear();

            ProjectItem[] messages = project.getMessageFileList();
            foreach (ProjectItem sFile in messages)
            {
                this.cmbMessageFiles.Items.Add(sFile.FileName);
                htMessages.Add(sFile.FileName, sFile);
            }

            if (this.cmbMessageFiles.Items.Count > 0)
            {
                this.cmbMessageFiles.SelectedIndex = 0;
                toolStripEvaluate.Enabled = true;
            }
            else
                toolStripEvaluate.Enabled = false;
        }

        private void toolStripClear_Click(object sender, EventArgs e)
        {
            txtEvaluate.Clear();
            txtStatus.Clear();
        }

        private void toolStripEvaluate_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                string sSelectedFile = (string)this.cmbMessageFiles.SelectedItem;
                if (htMessages.ContainsKey(sSelectedFile))
                {
                    ProjectItem i = (ProjectItem)htMessages[sSelectedFile];
                    string sFile = i.Path + i.FileName;

                    XmlDocument xInputMessage = new XmlDocument();
                    xInputMessage.Load(sFile); //TODO: Need to cache file instead of loading each time

                    txtStatus.Text = 
                        string.Format("Result: {0}" ,mt.evaluateExpression(xInputMessage.OuterXml, txtEvaluate.Text.Trim()));
                }
            }
            catch (Exception ex)
            {
                txtStatus.Text = 
                    string.Format("Error: {0}",ex.Message);
            }

            
        }

        

        private void cmbMessageFiles_SelectedIndexChanged(object sender, EventArgs e)
        {

            cmbMessageFiles.ToolTipText = (string)cmbMessageFiles.Items[cmbMessageFiles.SelectedIndex];
        }

        #endregion
    }
}